function webRestExplorer() { };
webRestExplorer.version = '1.1.0.0';
webRestExplorer.currentHttpMethod = 'GET';
webRestExplorer.errorMessageStartsWith = "";
webRestExplorer.errorHandler = function errorHandler(message, status, httpRequest) { alert(message.length > 0 ? message : httpRequest.statusText); };
webRestExplorer.jsonSettings = {
    "autoParse": false,
    "parseJson": function (data) {
        if (typeof ($) != "undefined") {
            return $.parseJSON(data);
        }
        else {
            return eval('(' + data + ')');
        }
    }
};
//client-side processing
webRestExplorer.getDataFromServer = function getDataFromServer(url, callback, extraParams) {
    // code for Mozilla, etc.
    var objHttp;
    if (window.XMLHttpRequest) {
        objHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {// code for IE
        objHttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
    if (objHttp == null) {
        alert('Browser problem: Unable to create XMLHTTP object!');
    }
    else {
        objHttp.onreadystatechange = function () {
            if (objHttp.readyState == 4 || objHttp.readyState == 'complete') {
                if (webRestExplorer.errorMessageStartsWith != "" && objHttp.responseText.toString().substring(0, webRestExplorer.errorMessageStartsWith.length) == webRestExplorer.errorMessageStartsWith) {
                    webRestExplorer.errorHandler(objHttp.responseText, objHttp.status, objHttp);
                }
                else if ((objHttp.status >= 200 && objHttp.status <= 299) || (objHttp.status >= 300 && objHttp.status <= 399)) {
                    var responseData = objHttp.responseText;
                    //convert to object from JSON if requested
                    var content = objHttp.getResponseHeader("Content-Type");
                    var isJson = false;
                    if (content != null && typeof (content) != "undefined") {
                        isJson = (content.toLowerCase().indexOf("json") >= 0);
                        if (isJson && webRestExplorer.jsonSettings.autoParse) {
                            responseData = webRestExplorer.jsonSettings.parseJson(objHttp.responseText);
                        }
                    }
                    if (extraParams != null && typeof (extraParams) != "undefined") {
                        extraParams.unshift(responseData);
                        callback.apply(this, extraParams);
                    }
                    else {
                        callback(responseData);
                    }
                }
                else {
                    webRestExplorer.errorHandler(objHttp.responseText, objHttp.status, objHttp);
                }
            }
        }
        this.currentHttpMethod = this.currentHttpMethod.toUpperCase();
        var formdata = '';
        if (this.currentHttpMethod == 'GET' || this.currentHttpMethod == 'DELETE') {
            url = url + "&rsid=" + this.generateSid();
            objHttp.open(this.currentHttpMethod, url, true);
        }
        else { //POST, PUT
            var posAmp = url.indexOf('&', 0);
            if (posAmp >= 0) {
                formdata = url.substring(posAmp + 1, url.length);
                url = url.substring(0, posAmp);
            }
            url = url + "&rsid=" + this.generateSid();
            objHttp.open(this.currentHttpMethod, url, true);
            objHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        objHttp.send(formdata);
    }
};
webRestExplorer.generateSid = function generateSid() {
    var randomnumber = Math.floor(Math.random() * 1000001);
    var sid = webRestSID + randomnumber.toString();
    return sid;
};
webRestExplorer.defaultCallback = function defaultCallback(data){};

webRestExplorer.topPosition = 0;
webRestExplorer.leftPosition = 0;
webRestExplorer.calculateCurrentPosition = function calculateCurrentPosition(oEvent) {
    topPosition = 0;
    leftPosition = 0;
    if (oEvent == null) { return; }

    //for NS
    if (oEvent.pageY || oEvent.pageX) {
        topPosition = oEvent.pageY;
        leftPosition = oEvent.pageX;
        return;
    }

    //calculate
    var oNode = oEvent.srcElement;
    while (oNode.tagName != "BODY") {
        topPosition += oNode.offsetTop;
        leftPosition += oNode.offsetLeft;
        oNode = oNode.offsetParent;
    }
};
webRestExplorer.registerNS = function registerNS(ns) {
    var nsParts = ns.split(".");
    var root = window;

    for (var i = 0; i < nsParts.length; i++) {
        if (typeof root[nsParts[i]] == "undefined") {
            root[nsParts[i]] = new Object();
        }
        root = root[nsParts[i]];
    }
};

